package com.jiazhong.servlet.controller;

import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseController extends HttpServlet {

    @SneakyThrows
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        System.out.println("BaseController method:" + method);
        // 1.获取到类
        Class<?> c = this.getClass();
        System.out.println("BaseController class:" + c);
        // 2.产生对象（获取到构造方法）
        Object obj = c.getDeclaredConstructor().newInstance();
        System.out.println("BaseController obj:" + obj);
        // 3.调用具体方法
        Method declaredMethod = c.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
        System.out.println("BaseController declaredMethod:" + declaredMethod);
        declaredMethod.invoke(obj, request, response);
    }
}
